package com.haihui.platform.controller.roles;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.haihui.platform.controller.BaseController;
import com.haihui.platform.entity.Resource;
import com.haihui.platform.service.roles.ResourceService;

/*
 * 资源
 */
@Controller
@RequestMapping("/res")
public class ResourceController extends BaseController{

	private static final Logger LOG = Logger.getLogger(ResourceController.class);
	
	@Autowired
	private ResourceService resService;
	
	/**
	 * 新增资源
	 * @param res
	 */
	@RequestMapping(value="/add")
	public void add(Resource res){
		try{

			int count = resService.add(res);
			this.responseWriter(this.getCommonJsonString(true, "新增成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "新增失败"));
		}
	}
	
	/**
	 * 修改资源
	 * @param res
	 */
	@RequestMapping(value="/update")
	public void update(Resource res){
		try{
			int count = resService.update(res);
			this.responseWriter(this.getCommonJsonString(true, "修改成功", count));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "修改失败"));
		}
	}
	
	/**
	 * 资源列表
	 * @param res
	 */
	@RequestMapping(value="/list")
	public void list(Resource res){
		try{
			int count = resService.count(res);
			List<Resource> list = resService.list(res);
			this.responseWriter(this.getCommonJsonPage(count,list));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}
	
	/**
	 * 所有资源
	 * @param
	 */
	@RequestMapping(value="/getAll")
	public void getAll(){
		try{
			List<Resource> list = resService.getAll();
			this.responseWriter(this.getCommonJsonString(true, "查询成功", list));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}
	
	/**
	 * 资源列表
	 * @param res
	 */
	@RequestMapping(value="/detail")
	public void detail(Resource res){
		try{
			Resource map = resService.detail(res);
			this.responseWriter(this.getCommonJsonString(true, "查询成功", map));
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}

	@RequestMapping({"/getParent"})
	public void getResourceParent(Long filterId) throws Exception {
		List resources = resService.getParentTree(filterId);
		this.responseWriter(this.getCommonJsonString(Boolean.valueOf(true), "获取资源树成功", resources));
	}
	
	/**
	 * 根据角色查资源
	 * @param roleId 角色id
	 */
	@RequestMapping(value="/resByRole")
	public void resByRole(@RequestParam(value="roleId") String roleId){
		try{
			List<Map<String, Object>> list = resService.resByRole(roleId);
			if(list != null){
				if(list.size() > 0){
					String[] arr = new String[list.size()];
					for(int i = 0; i < list.size(); i++){
						arr[i] = list.get(i).get("res_id").toString();
					}
					this.responseWriter(this.getCommonJsonString(true, "查询成功", arr));
				}else{
					this.responseWriter(this.getCommonJsonString(true, "查询成功", new String[]{}));
				}
			}else{
				this.responseWriter(this.getCommonJsonString(false, "查询失败"));
			}
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			this.responseWriter(this.getCommonJsonString(false, "查询失败"));
		}
	}

	
}
